from django.shortcuts import render

from django.http import HttpResponse, JsonResponse
from platform_config.models import PlatformConfig
import  time
from pang5_web.func  import get_uid
# Create your views here.
from pang5_web.settings import  DEBUG

def config_list(request):
    if request.method != 'GET':
        return HttpResponse(status=404)
    page_num = int(request.GET.get('page_num', 1))
    page_size = 200
    min = (page_num - 1) * page_size
    max = page_num * page_size
    ret = list(PlatformConfig.objects.order_by('id').values()[min:max])
    if  not get_uid(request.COOKIES.get('pang5','')):
        #位登录
        return JsonResponse({'status':403,'msg':"用户未登录"},safe=False)
    return JsonResponse(ret, safe=False)


def config_add_or_change(request):
    res = {}
    if request.method != 'POST':
        return HttpResponse(status=404)
    id = request.POST.get('id', 0)
    platform = request.POST.get('platform', '')
    platform_en = request.POST.get('platform_en', '')
    work_type = request.POST.get('work_type', '')
    has_cover_img = request.POST.get('has_cover_img', '')

    cover_img_size = request.POST.get('cover_img_size', '')
    cover_img_width = request.POST.get('cover_img_width', '')
    cover_img_height = request.POST.get('cover_img_height', '')
    chapter_img_1_size = request.POST.get('chapter_img_1_size', '')
    chapter_img_1_width = request.POST.get('chapter_img_1_width', '')
    chapter_img_1_height = request.POST.get('chapter_img_1_height', '')
    chapter_img_2_size = request.POST.get('chapter_img_2_size', '')
    chapter_img_2_width = request.POST.get('chapter_img_2_width', '')
    chapter_img_2_height = request.POST.get('chapter_img_2_height', '')
    platform_logo_addr = request.POST.get('platform_logo_addr', '')
    support_login_types = request.POST.get('support_login_types', '')
    third_id = request.POST.get('third_id', 0)

    if not int(id):
        PlatformConfig.objects.create(platform=platform, platform_en=platform_en,
                                      work_type=work_type, has_cover_img=has_cover_img, cover_img_size=cover_img_size,
                                      cover_img_width=cover_img_width, cover_img_height=cover_img_height,
                                      chapter_img_1_size=chapter_img_1_size, chapter_img_1_width=chapter_img_1_width,
                                      chapter_img_1_height=chapter_img_1_height, chapter_img_2_size=chapter_img_2_size,
                                      chapter_img_2_width=chapter_img_2_width,
                                      chapter_img_2_height=chapter_img_2_height, platform_logo_addr=platform_logo_addr,
                                      support_login_types=support_login_types)
        res = {'status': 0, 'msg': '创建成功'}
    else:

        l = PlatformConfig.objects.filter(id=id).all()
        if len(l) == 0:
            res = {'status': -1, 'msg': '数据不存在'}
        else:

            PlatformConfig.objects.filter(id=id).update(platform=platform,
                                                        work_type=work_type,
                                                        has_cover_img=has_cover_img, cover_img_size=cover_img_size,
                                                        cover_img_width=cover_img_width,
                                                        cover_img_height=cover_img_height,
                                                        chapter_img_1_size=chapter_img_1_size,
                                                        chapter_img_1_width=chapter_img_1_width,
                                                        chapter_img_1_height=chapter_img_1_height,
                                                        chapter_img_2_size=chapter_img_2_size,
                                                        chapter_img_2_width=chapter_img_2_width,
                                                        chapter_img_2_height=chapter_img_2_height,
                                                        platform_logo_addr=platform_logo_addr,
                                                        support_login_types=support_login_types,third_id=third_id)
            res = {'status': 0, 'msg': '修改成功'}
    return JsonResponse(res, safe=False)


def config_delete(request):
    if request.method != 'GET':
        return HttpResponse(status=404)
    id = int(request.GET.get('id', 0))
    if not id:
        ret = {'status': '-1', 'msg': 'id不存在'}
        return JsonResponse(ret, safe=False)
    find_by_id = PlatformConfig.objects.filter(id=id).all()
    if len(find_by_id) == 0:
        ret = {'status': '-1', 'msg': '该信息不存在'}
    else:
        find_by_id[0].delete()
        ret = {'status': 0, 'msg': '删除成功'}
    return JsonResponse(ret, safe=False)


def index(request):
    uid =get_uid(request.COOKIES.get('pang5',0))
    if not DEBUG:
        if uid != 102728 :
            return HttpResponse(status=500)
    return render(request, 'platform_config/index.html', None)
